package com.cz.kingdee.prod;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.cz.kingdee.KingdeeClient;
import com.cz.kingdee.dto.PrdMorpt;
import com.kingdee.bos.webapi.entity.OperateParam;
import com.kingdee.bos.webapi.entity.OperatorResult;
import com.kingdee.bos.webapi.entity.QueryParam;
import com.kingdee.bos.webapi.sdk.K3CloudApi;


import java.util.ArrayList;
import java.util.List;

/**
 *  审核生产汇报单
 */
public class AuditPrdMorpt {

    public static void main(String[] args) {
        try {
            KingdeeClient client = new KingdeeClient();
            K3CloudApi k3CloudApi = client.getProdKingdeeClient();
            QueryParam queryParam = new QueryParam();
            queryParam.setFormId("PRD_MORPT");
            queryParam.setFieldKeys("FID,FBillNo,FCreateDate,FDocumentStatus");
            //AND FDocumentStatus='C'
            //queryParam.setFilterString("FDate>='2025-01-01' AND FDate<='2025-01-31' AND FCreatorId='28859605' AND FWorkshipId='2539006'");
            queryParam.setFilterString("FDate>='2025-01-01' AND FDate<='2025-01-31'");
            queryParam.setFilterString("FDocumentStatus='A'");
            //queryParam.setStartRow(0);
            //queryParam.setLimit(10000);

            List<PrdMorpt> result = k3CloudApi.executeBillQuery(queryParam, PrdMorpt.class);
            if(CollUtil.isNotEmpty(result)){
                for (PrdMorpt prdMorpt : result) {
                    String billNo = prdMorpt.getFBillNo();
                    List<String> numbers = new ArrayList<>();
                    numbers.add(billNo);

                    OperateParam operateParam = new OperateParam();
                    operateParam.setNumbers(numbers);
                    OperatorResult operatorResult = null;
                    try {
                        operatorResult = k3CloudApi.submit("PRD_MORPT", operateParam);
                        System.out.println("提交结果:" + JSONUtil.toJsonStr(operatorResult.getResult().getResponseStatus()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            if(CollUtil.isNotEmpty(result)){
                for (PrdMorpt prdMorpt : result) {
                    String billNo = prdMorpt.getFBillNo();
                    List<String> numbers = new ArrayList<>();
                    numbers.add(billNo);

                    OperateParam operateParam = new OperateParam();
                    operateParam.setNumbers(numbers);
                    OperatorResult operatorResult = null;
                    try {
                        operatorResult = k3CloudApi.audit("PRD_MORPT", operateParam);
                        System.out.println("审核结果:" + JSONUtil.toJsonStr(operatorResult.getResult().getResponseStatus()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
